package com.enfi.api.modules.bz.controller;

import com.enfi.api.domain.res.ApiResult;
import com.enfi.api.modules.db.service.WarningViewService;
import com.enfi.api.modules.db.service.WorkorderUserService;
import com.enfi.api.modules.db.vo.WorkorderUserVO;
import com.enfi.api.util.SecUser;
import com.enfi.api.util.UserUtils;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.Predicate;
import java.util.List;

/**
 * @author wind
 * @date 2020/4/10
 */
@RestController
@RequestMapping({"user/count"})
public class UserCountController {
    @Autowired
    private WarningViewService warningViewService;
    @Autowired
    private WorkorderUserService workorderUserService;
    @GetMapping("warning")
    public Object count() {
        SecUser secUser= UserUtils.getSecUser();
        Long count = warningViewService.count((root, query, builder) -> {
                    List<Predicate> predicates = Lists.newArrayList();
                    predicates.add(builder.equal(root.get("userId"), secUser.getId()));
                    predicates.add(builder.equal(root.get("viewStatus"), 0));
                    query.where(predicates.toArray(new Predicate[predicates.size()]));
                    return null;
                }
        );
        return ApiResult.renderOk().put("count", count);
    }

    @GetMapping("workorder")
    @ResponseBody
    public Object workorderCount() {
        SecUser secUser= UserUtils.getSecUser();
        Long count = workorderUserService.count((root, query, builder) -> {
                    List<Predicate> predicates = Lists.newArrayList();
                    predicates.add(builder.equal(root.get("userId"), secUser.getId()));
                    query.where(predicates.toArray(new Predicate[predicates.size()]));
                    return null;
                }
        );
        return ApiResult.renderOk().put("count", count);
    }
}
